package com.mike.bdb.ui.provider.tree;

import org.eclipse.jface.viewers.ISelection;

import com.mike.bdb.manager.DBManager;

public class TreeSelectionUtil {
	
	private static final String SPLIT = DBManager.BERKELEY_DATABASE_SECONDARY_INDEX_SPLIT_CHAR;
	private static final String DPL = DBManager.BERKELEY_DATABASE_DPL;
	
	public static String getName(String name) {
		if(null == name || "".equals(name.trim())) return null;
		return name.substring(1, name.length() - 1);
	}
	
	public static String getName(ISelection selection) {
		if(null == selection) return null;
		String string = selection.toString();
		return string.substring(1, string.length() - 1);
	}

	public static String getDatabaseName(ISelection selection) {
		return getDatabaseName(getName(selection));
	}
	
	public static String getDatabaseClassName(String name) {
		if(null == name) return null;
		if(name.contains(TreeNode.LEAF_NODE_SPLIT_CHAR))
			name = name.split(TreeNode.LEAF_NODE_SPLIT_CHAR)[1];
		name = name.replace(DPL + SPLIT + DBManager.getInstance().getStore() + SPLIT, "");
		return name;
	}
	
	public static String getDatabaseName(String name) {
		if(null == name) return null;
		if(name.contains(TreeNode.LEAF_NODE_SPLIT_CHAR))
			name = name.split(TreeNode.LEAF_NODE_SPLIT_CHAR)[0];
		if(name.contains(TreeNode.NODE_SPLIT_CHAR))
			name = name.split(TreeNode.NODE_SPLIT_CHAR)[0];
		return name;
	}
	
	
}
